package com.taoge.p4.common.config;

import com.taoge.p4.common.interceptor.ApiInterceptor;
import com.taoge.p4.common.interceptor.SysInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private ApiInterceptor apiInterceptor;

    @Resource
    private SysInterceptor sysInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if ("taoge-api".equals(applicationName)) {
            // api项目走api的拦截器
            registry.addInterceptor(apiInterceptor)
                    .addPathPatterns("/**")
                    .excludePathPatterns("/static/**");
        } else if ("taoge-sys".equals(applicationName)) {
            // sys项目走sys的拦截器
            registry.addInterceptor(sysInterceptor)
                    .addPathPatterns("/**")
                    .excludePathPatterns("/static/**");
        }

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
